package cn.aishua.data;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import cn.aishua.data.PostsDataParser.PostsVendor.MerchantTag;
import cn.aishua.util.FormatConverter;

public class PostsDataParser
{

    public ArrayList<PostsVendor> data = new ArrayList<PostsVendor>();
    public int count = 0;

    public ArrayList<PostsVendor> pData = new ArrayList<PostsVendor>();
    public int pCount = 0;
    final int PAGE_SIZE = 10;
    private int position = 0;

    public PostsDataParser(String raw)
    {
        parse( raw );
    }

    private void parse( String r )
    {

        try
        {
            JSONObject obj = new JSONObject( r );
            JSONArray a = obj.getJSONArray( "stores" );

            for( int i = 0; i < a.length(); i++ )
            {
                JSONObject temp = a.getJSONObject( i );
                data.add( makeVendor( temp ) );
                android.util.Log.e("test", "add one");
                count++;
            }
        }
        catch( JSONException e )
        {
            e.printStackTrace();
        }
    }

    public boolean refresh()
    {
        int newPosition = ( ( position + PAGE_SIZE ) > count ? count : ( position + PAGE_SIZE ) );
        for( int i = position; i < newPosition; i++ )
        {
            pData.add( i, data.get( i ) );
            pCount++;
        }
        position = newPosition;

        return position == count;
    }

    private PostsVendor makeVendor( JSONObject temp )
    {
        PostsVendor obj = new PostsVendor();

        try
        {
            obj.id = temp.getInt( "id" );
            android.util.Log.d( "test", "id " + obj.id );
            obj.name = temp.getString( "name" );
            obj.average_price = (float) temp.getDouble( "average_price" );
            obj.max_customer_rate = (float) temp.getDouble( "max_customer_rate" );
            obj.file_url = temp.optString( "file_url", "" );
            
            android.util.Log.d( "test", "distance " + temp.getString( "distance" ) );
            obj.distance = Integer.valueOf( FormatConverter.toIntFormat( temp.getString( "distance" ) ) );

            JSONArray a = temp.getJSONArray( "merchant_tags" );
            for( int i = 0; i < a.length(); i++ )
            {
                obj.merchant_tags.add( new MerchantTag(a.getJSONObject( i ).getString( "name" ), a.getJSONObject( i ).getString( "tag_type" )) );
                Log.e("test", "merchant_tags.add name " + a.getJSONObject( i ).getString( "name" ) +
                		", type " + a.getJSONObject( i ).getString( "tag_type" ));
            }

//            obj.business_circle = temp.getJSONObject( "business_circle" ).getString( "name" );

            JSONArray b = temp.getJSONArray( "sub_category" );
            for( int i = 0; i < b.length(); i++ )
            {
                obj.sub_category.add( b.getJSONObject( i ).getString( "name" ) );
            }

            obj.lati = temp.getString( "gps_x" );
            obj.longt = temp.getString( "gpx_y" );

        }
        catch( JSONException e )
        {
            e.printStackTrace();
        }
        return obj;
    }

    public static class PostsVendor
    {
        public int id;
        public String name;
        public float average_price;
        public float max_customer_rate;
        public String file_url;
        public int distance;
        public ArrayList<MerchantTag> merchant_tags = new ArrayList<MerchantTag>();
        public String business_circle;
        public ArrayList<String> sub_category = new ArrayList<String>();
        public String lati;
        public String longt;
        
        public static class MerchantTag
        {
        	public String name;
        	public String type;
        	public MerchantTag(String n, String t)
        	{
        		name = n;
        		type = t;
        	}
        }
        
    }

}
